package com.leon.alishop.core.bof.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.leon.alishop.core.bo.GoodsSkuBo;
import com.leon.alishop.core.bof.GoodsSkuBof;
import com.leon.alishop.core.enumeration.AttrInfoTypeEnum;
import com.leon.alishop.core.model.AttributesModel;
import com.leon.alishop.core.service.AttributesService;
import com.leon.alishop.dao.GoodsSkuDao;
import com.leon.alishop.model.AttrValInfo;
import com.leon.alishop.model.GoodsSku;
import com.leon.alishop.model.ProviderInfo;

@Service("goodsSkuBof")
public class GoodsSkuBofImpl implements GoodsSkuBof{
	private static Logger log = LoggerFactory
			.getLogger(GoodsSkuBofImpl.class);
	@Override
	public List<GoodsSkuBo> parseGoodsSkuBo(ProviderInfo provider,List<GoodsSku> goodsSkuList) {
		if(null == goodsSkuList)
			return null;
		List<GoodsSkuBo> ret = new ArrayList<GoodsSkuBo>();
		GoodsSkuBo skuBo = null;
		AttributesModel colorAttr = attributesService.findAttributeByNameAndType("颜色", AttrInfoTypeEnum.COLOR.ordinal());
		AttributesModel sizeAttr = attributesService.findAttributeByNameAndType("尺码", AttrInfoTypeEnum.SIZE.ordinal());
		AttrValInfo avi;
		for (GoodsSku sku : goodsSkuList) {
			skuBo = new GoodsSkuBo();
			BeanUtils.copyProperties(sku, skuBo);
			if(null != provider){
				skuBo.setSalePrice(provider.getSalePrice(sku.getProviderSkuPrice()));
			}
			avi = colorAttr.getAttrValInfo(skuBo.getColorAttrValId());
			if(avi == null){
				log.error("无效的colorAttrValId:"+skuBo.getColorAttrValId());
			}else{
				skuBo.setColorAttrValue(avi.getValueName());
			}
			avi = sizeAttr.getAttrValInfo(skuBo.getSizeAttrValId());
			if(avi == null){
				log.error("无效的sizeAttrValId:"+skuBo.getSizeAttrValId());
			}else{
				skuBo.setSizeAttrValue(avi.getValueName());
			}
			ret.add(skuBo);
		}
		return ret;
	}
	
	@Override
	public List<List<GoodsSkuBo>> parseGoodsSkuBoTable(ProviderInfo provider,List<GoodsSku> goodsSkuList) {
		List<GoodsSkuBo> goodsSkuBoList = this.parseGoodsSkuBo(provider,goodsSkuList);
		if(null == goodsSkuBoList)
			return null;
		List<List<GoodsSkuBo>> ret = new ArrayList<List<GoodsSkuBo>>();
		Map<Integer, GoodsSkuBo> sizeGsbMap = new LinkedHashMap<Integer, GoodsSkuBo>();
		Map<Integer, GoodsSkuBo> colorGsbMap = new LinkedHashMap<Integer, GoodsSkuBo>();
		AttrValInfo avi;
		Map<String,GoodsSkuBo> goodsSkuBoMap = new HashMap<String, GoodsSkuBo>();
		for (GoodsSkuBo gsb : goodsSkuBoList) {
			if(!sizeGsbMap.containsKey(gsb.getSizeAttrValId())){
				avi = new AttrValInfo();
				avi.setId(gsb.getSizeAttrValId());
				avi.setValueName(gsb.getSizeAttrValue());
				sizeGsbMap.put(gsb.getSizeAttrValId(),gsb);
			}
			if(!colorGsbMap.containsKey(gsb.getColorAttrValId())){
				avi = new AttrValInfo();
				avi.setId(gsb.getColorAttrValId());
				avi.setValueName(gsb.getColorAttrValue());
				colorGsbMap.put(gsb.getColorAttrValId(),gsb);
			}
			goodsSkuBoMap.put(gsb.getSizeAttrValId() + "_" + gsb.getColorAttrValId(), gsb);
		}
		// 构造表格
		List<GoodsSkuBo> gsbList;
		GoodsSkuBo gsb;
		String key;
		for (GoodsSkuBo colorGsb : colorGsbMap.values()) {
			gsbList = new ArrayList<GoodsSkuBo>();
			for (GoodsSkuBo sizeGsb : sizeGsbMap.values()) {
				key = sizeGsb.getSizeAttrValId() + "_"+ colorGsb.getColorAttrValId();
				if(goodsSkuBoMap.containsKey(key)){
					gsb = goodsSkuBoMap.get(key);
				}else{
					gsb = new GoodsSkuBo();
					gsb.setSizeAttrValId(sizeGsb.getSizeAttrValId());
					gsb.setColorAttrValId(colorGsb.getColorAttrValId());
					gsb.setSizeAttrValue(sizeGsb.getSizeAttrValue());
					gsb.setColorAttrValue(colorGsb.getColorAttrValue());
				}
				gsbList.add(gsb);
			}
			ret.add(gsbList);
		}
		
		return ret;
	}

	
	@Autowired
	private GoodsSkuDao goodsSkuDao;
	
	@Autowired
	private AttributesService attributesService;

	
	
}
